package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 广告位管理
 */
@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private IYddAdvposService yddAdvposService;
    @Autowired
    private IYddAdvimgService yddAdvimgService;

    /**
     * 广告位列表-分页
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, Integer page, Integer limit) {
        Page<YddAdvpos> advposPage = new Page<>(page, limit);
        Page<YddAdvposDto> dtoPage = new Page<>();
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        lqw.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);
        lqw.orderByDesc(YddAdvpos::getId);
        yddAdvposService.page(advposPage, lqw);

        List<YddAdvpos> records = advposPage.getRecords();
        List<YddAdvposDto> yddAdvposDtoList = new ArrayList<>();
        records.forEach(yddAdvpos -> {
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(yddAdvpos, yddAdvposDto);
            LambdaQueryWrapper<YddAdvimg> qw = new LambdaQueryWrapper<>();
            qw.eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId());
            int count = yddAdvimgService.count(qw);
            yddAdvposDto.setAdvposnum(count);
            yddAdvposDtoList.add(yddAdvposDto);
        });
        dtoPage.setRecords(yddAdvposDtoList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count", (int) advposPage.getTotal());  //总数
        map.put("totalPages", (int) advposPage.getPages()); //总页数
        map.put("pageSize", (int) advposPage.getSize()); //每页数量
        map.put("currentPage", (int) advposPage.getCurrent()); //每页页码
        map.put("data", dtoPage.getRecords());

        return ResultData.ok("", map);
    }

    /**
     * 广告位修改
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos yddAdvpos) {
        log.info("广告位修改:{}", yddAdvpos);
        boolean update = yddAdvposService.updateById(yddAdvpos);
        if (update) {
            return R.success("", "修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delete(Integer id) {
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, YddAdvpos::getId, id);
        YddAdvpos yddAdvpos = yddAdvposService.getOne(lqw);

        if (yddAdvpos.getDefaultData() != null) {
            return R.error("系统默认数据,不能删除");
        }

        LambdaQueryWrapper<YddAdvimg> qw = new LambdaQueryWrapper<>();
        qw.eq(id != null, YddAdvimg::getAdvimgpos, id);
        int count = yddAdvimgService.count(qw);
        if (count > 0) {
            throw new BusinessException("广告位下还有广告图,不能删除!");
        }
        yddAdvposService.removeById(id);
        return R.success("", "删除成功");
    }

    /**
     * 广告位列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("", list);
    }

    /**
     * 广告位详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        if (yddAdvpos != null) {
            return ResultData.ok("0", yddAdvpos);
        }
        return ResultData.error("没有查询到对应信息");
    }

    /**
     * 广告位添加
     *
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos yddAdvpos) {
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddAdvpos::getAdvposname, yddAdvpos.getAdvposname());
        YddAdvpos one = yddAdvposService.getOne(lqw);
        if (one != null) {
            return R.error("广告位名称已存在,请重试!");
        }
        boolean save = yddAdvposService.save(yddAdvpos);
        if (save) {
            return R.success("", "广告位添加成功");
        }
        return R.error("广告位添加失败");
    }

}
